home *** CD-ROM | disk | FTP | other *** search
/ LSD Docs / LSD Docs.iso / FILEZ / lsd15.dms / lsd15.adf / Ctutorial1.doc.pp / Ctutorial1.doc
Text File  |  1991-02-19  |  15KB  |  398 lines

  1.                     ALL AT C - ANOTHER C TUTORIAL
  2.  
  3. By Andy P++
  4.  
  5.  
  6. Do you want to learn to  program  in C?  Or maybe you would
  7. like to  understand  the  Intuition  Reference  manual?  Or
  8. perhaps you  are  just  curious  about  why  the  gurus  at
  9. Commodore decided it would be a  "good  thing" to use C for
  10. major parts of  the  software  development  for  the Amiga.
  11. Then read on.
  12.  
  13. In this series of  articles  I  hope  to  introduce all the
  14. major features of C in the  context of the Amiga.  I assume
  15. that most readers are familiar  with  at least some form of
  16. programming... even BASIC!
  17.  
  18. What is C?
  19.         Wow! the tricky stuff first.   Well, C is the third
  20.         letter of the alphabet  (our  alphabet anyway), but
  21.         it is also a name  for a programming language.  The
  22.         language was developed  by  a  small  team  at Bell
  23.         Laboratories.  It was created in response to a need
  24.         for a language to implement a new operating system,
  25.         UNIX.
  26.  
  27.         The  language  itself  was   based  on  a  previous
  28.         language developed by Ken Thompson in 1970 called B
  29.         (hence C...  now you know!).   B  itself owed a lot
  30.         to  a  previous   language,   BCPL,   developed  at
  31.         Cambridge University in  1967  by  Martin Richards.
  32.         BCPL stands  for  Basic  CPL  (Combined Programming
  33.         Language).  It is a curious coincidence that, apart
  34.         from assembler, the Amigas  system software was all
  35.         written either in C or BCPL!
  36.  
  37. OK, but what makes C different from BASIC or assembler?
  38.         Lots, but it may  help  to  categorise  C as a low-
  39.         level language, some  have  even  said a structured
  40.         assembler.
  41.  
  42.         BASIC is a much higher  level language in the sense
  43.         that the  user  is  much  more  insulated  from the
  44.         hardware.  You BASIC  programmers  out there (there
  45.         are a few left I've been  told) are going to have a
  46.         much harder time with C than  those of you who have
  47.         hacked it at the machine level.
  48.  
  49.         Another thing that  distinguishes  C  from BASIC is
  50.         that C is  almost  exclusively  compiled.   That is
  51.         that the program is  written first, then translated
  52.         to  machine  code,   then   run.    I   say  almost
  53.         exclusively  since  I   have   seen  interpreted  C
  54.         advertised... this seems  a  case  of  the worst of
  55.         both worlds!
  56.  
  57.         Although closer in spirit  to  assembler, C is more
  58.         of  a   symbolic   language,   representing   basic
  59.         operations in a (nearly) portable way.
  60.  
  61. OK, Quit the bovine excrement, show us a program
  62.         Alright, you asked for it...
  63.  
  64.                 main()
  65.                 {
  66.                         printf( "Hello world\n");
  67.                 }
  68.  
  69.         That was a complete  program  to output the message
  70.         'Hello world' followed by a  newline to the screen.
  71.         To BASIC boys, yes I  know  it's  a bit verbose, to
  72.         assembler hackers, yes I know it's a bit brief.
  73.  
  74.         Although easy to write you  will go through 7 kinds
  75.         of hell trying to compile  and  run  this.  It is a
  76.         characteristic of  this  type  of  programming  (as
  77.         opposed to BASIC) that a short program is nearly as
  78.         tricky as a medium sized one.
  79.  
  80.         Let me try to explain the elements of this "simple"
  81.         program.
  82.  
  83.         The word  "main()"  introduces  the  start  of  the
  84.         program, it says "this is  the  first thing to do".
  85.         Later I  will  explain  about  functions,  and  the
  86.         meaning of the empty brackets should become clear.
  87.  
  88.         The curly brackets "{" and  "}" are used to collect
  89.         together a set of  things,  in  this  case a set of
  90.         "statements" (equivalent to lines of basic code, or
  91.         lines of assembler).
  92.  
  93.         The only statement in this program is:
  94.                  printf("Hello World\n");
  95.  
  96.         This says print the text  'Hello world' followed by
  97.         a newline ( '\n' is C-speak for a newline).
  98.  
  99. Most impressive, but there must be more
  100.         Well yes, but to explain  much  more I will have to
  101.         go through a few fundamentals  of C.  At this point
  102.         things will probably seem to  be a bit disconnected
  103.         but I will try to  show  an  example  at the end to
  104.         draw the threads together.
  105.  
  106. Data types
  107.         C can handle various sorts  of data.  The principle
  108.         simple types are integers  (0,1,2  etc, even -1, -2
  109.         and so on if you can  handle that), characters ( A,
  110.         B, C and so on) and floating point numbers (24.678,
  111.         0.00045, and many  others).   These  are  the basic
  112.         sorts of thing that C deals with, and it has a name
  113.         for each of them.
  114.  
  115.         There  are  other   types   but   they  are  either
  116.         variations  on  a  theme  (for  example  signed  or
  117.         unsigned numbers),  or  collections  of  the simple
  118.         types (arrays etc.).
  119.  
  120.         In common with assembler, C insists that the person
  121.         writing the program  tells  the  compiler about the
  122.         data used,  its  name  and  its  type.   Such named
  123.         pieces of  data  are  called  "variables",  each of
  124.         which can contain a  range  of values determined by
  125.         the type.
  126.  
  127.         The predefined simple types are:
  128.  
  129.         TYPE    DESCRIPTION                     RANGE
  130.  
  131.         int     signed integers         -2147483648 to
  132.                                                 2147483647
  133.  
  134.         char    a character or byte      -128 to 127
  135.  
  136.         float   a "real" number         +/-10E-37 to
  137.                                                 +/-10E+38
  138.  
  139.         A few explanations here...
  140.  
  141.         Characters  are  represented  by   a  small  number
  142.         corresponding to the ASCII code, eg 'A' is 65.
  143.  
  144.         For the assembler folks, an int is held in 32 bits,
  145.         a char in 8 bits and a float in 32 bits.
  146.  
  147.         Until further notice I  will  ignore  floats, as in
  148.         many years of programming I have very rarely needed
  149.         them.
  150.  
  151.         Given these types, if you  want  to name a variable
  152.         you simply precede the  name  you  want to use with
  153.         the name of the type, for example:
  154.  
  155.                 int     a;
  156.  
  157.         Says the variable  'a'  is  an  integer.   C allows
  158.         quite a lot  of  flexibility  in  naming variables,
  159.         lower case, upper  case,  numbers  and underscores,
  160.         although  names  must  start   with   a  letter  or
  161.         underscore.
  162.  
  163.         So:
  164.  
  165.                 fred, Quite_A_Long_Name, A12343, _43
  166.  
  167.         are all OK names.
  168.  
  169.                 1Henry, 128, &fred, xy#z
  170.  
  171.         would all be rejected.
  172.  
  173. Statements
  174.         As well as data, a complete program uses statements
  175.         to tell the computer what to do.
  176.  
  177.         In C there  are  a  range  of  statement  types, we
  178.         introduce a couple here.
  179.  
  180.         Assignment:
  181.  
  182.             To change the value  of  a  variable we usually
  183.             use an  assignment  statement.   This  uses the
  184.             equals sign.  Assume we  have a variable called
  185.             'nissan' that is of type  'int', that is it can
  186.             contain integers.  Then to  put  the value 4096
  187.             in it we use the statement:
  188.  
  189.                  nissan = 4096;
  190.  
  191.             The semicolon says, "this  is  the  end of this
  192.             statement".
  193.  
  194.             What comes after the '='  sign does not have to
  195.             be a single thing, it could be an expression:
  196.  
  197.                  nissan = 4096 + 17;
  198.  
  199.             or  even   reference   another   variable,  say
  200.             'mitsubishi':
  201.  
  202.                  nissan = 4096 - mitsubishi;
  203.  
  204.  
  205.         While loop:
  206.  
  207.             The simplest loop in C is the "while" loop.  It
  208.             allows a statement or group of statements to be
  209.             repeated while some condition is true.
  210.  
  211.  
  212.             This looks like:
  213.  
  214.                  while( condition)
  215.                          statement;
  216.  
  217.             Or if we want  to  loop  through  more than one
  218.             statement use the curly brackets:
  219.  
  220.                  while( condition)
  221.                  {
  222.                          statement1;
  223.                          statement2;
  224.                  }
  225.  
  226. Comments
  227.         To help make C code  more  readable (some would say
  228.             "less    unreadable"!)    comments    may    be
  229.             interspersed with the code at most places where
  230.             a space could be used.
  231.  
  232.         A comment is  distinguished  by  being  enclosed in
  233.             comment brackets,  /*  ....  */.   The compiler
  234.             ignores  comments  entirely,  and  unlike  some
  235.             interpreted  languages  comments  do  not  slow
  236.             down, or make bigger, your program so feel free
  237.             to express yourself.
  238.  
  239. A more complete example
  240.         Using what we now know, lets write a simple program
  241.             to print out the numbers  1  to 5 and the cubes
  242.             of these numbers.
  243.  
  244.         main()  /* Remember, this says start here */
  245.         {
  246.             /* Declare an integer variable */
  247.             int a_number;
  248.             /* And another for its cube */
  249.             int cube;
  250.  
  251.             /* Initialise a number */
  252.             a_number = 1;
  253.  
  254.             while ( a_number <= 5)
  255.             {
  256.                 /* Calculate the cube */
  257.                 cube = a_number * a_number * a_number;
  258.  
  259.                 /* And print the result */
  260.                 printf("%d cubed is %d\n", a_number, cube);
  261.  
  262.                 /* Go on to the next number */
  263.                 a_number = a_number + 1;
  264.             }
  265.         }
  266.  
  267.         Quick word of  explanation  on  the  "printf" line.
  268.         "printf" is the nearest equivalent  C has to BASICs
  269.         PRINT command.  It is rather  more  ugly to look at
  270.         but can do very  similar  (and  in  some cases more
  271.         sophisticated)  things.   Rather  than  the  simple
  272.         print of  a  string  we  did  before,  now  we  are
  273.         printing numbers  inside  a  string.   Wherever the
  274.         "%d" is found, the  next  number  in  the list that
  275.         follows is displayed.
  276.  
  277.         The output  of  that  program  will  therefore look
  278.         like:
  279.  
  280.             1 cubed is 1
  281.             2 cubed is 8
  282.             3 cubed is 27
  283.             4 cubed is 64
  284.             5 cubed is 125
  285.  
  286.  
  287.         That seemed a bit windy, there will be those of you
  288.         out there saying, "Yes, but  I  thought C was terse
  289.         and obscure".  You would  of  course  be right in a
  290.         sense as  it  largely  depends  on  the programmer.
  291.         Putting my  C-hacker  hat  on  we  can  rewrite the
  292.         previous program as:
  293.  
  294.             main()
  295.             {
  296.                 int i=0;
  297.  
  298.                 while( ++i < 6)
  299.                     printf( "%d cubed is %d\n", i, i*i*i);
  300.             }
  301.  
  302.         This introduces only one  really new concept (apart
  303.         from the  fact  that  C  programmers  take  an  odd
  304.         pleasure in being needlessly  obscure)  and that is
  305.         the increment operator, the double plus sign.
  306.  
  307.         This operator is worthy  of  a  place in the "black
  308.         museum"  of  programming.   Its   advantage  is  in
  309.         producing  very  terse  programs  that  are  easily
  310.         translated to  quite  terse  object  programs.  Its
  311.         disadvantage is  that  badly  used  it  can  make a
  312.         program very difficult to understand.
  313.  
  314.         The effect of  the  ++  operator  is twofold.  When
  315.         placed in front of a variable  it first adds one to
  316.         the variable,  and  then  returns  that incremented
  317.         value.
  318.  
  319.         Assembler  programmers  will   understand   the  ++
  320.         operator  as  being   very   similar   to   an  INC
  321.         instruction.  Most high level languages do not have
  322.         a similar concept so the C statement:
  323.  
  324.                     charlie = ++fred;
  325.  
  326.             would in most  ordinary  languages be something
  327.         like:
  328.  
  329.                     fred = fred + 1;
  330.                     charlie = fred;
  331.  
  332.         Note that both examples there  were in fact valid C
  333.         and if you don't like them you can always avoid the
  334.         use of the  ++  operator.   Unfortunately  you will
  335.         probably need to read code using it.
  336.  
  337.         Before I wrap up this  first lesson (watch out K&R,
  338.         here I come!) lets just go through the full list of
  339.         simple data types (I previously simplified things).
  340.  
  341.           signed char            A  character in  the range
  342.                                  -128 to  +127,  also often
  343.                                  known simply as "char"
  344.  
  345.           unsigned char          A character in the range 0
  346.                                  to 255.
  347.  
  348.           signed short int       An  integer in   the range
  349.                                  -32768 to +32767.
  350.  
  351.           unsigned short int     An integer in  the range 0
  352.                                  to 65535.
  353.  
  354.           signed int             An integer  in  the  range
  355.                                  2147483648 to  2147483647,
  356.                                  also   known   simply   as
  357.                                  "int".
  358.  
  359.           unsigned int           An integer in  the range 0
  360.                                  to 4294987295.
  361.  
  362.           signed long int        As per "signed int".
  363.  
  364.           unsigned long int      As per "unsigned int".
  365.  
  366.           float                  A floating point number in
  367.                                  the range +/-10E-37 to +/-
  368.                                  10E+38.  A  low  precision
  369.                                  floating point number.
  370.  
  371.           double                 A floating point number in
  372.                                  the  range  +/-10E-307  to
  373.                                  +/-10E+308.
  374.                                  A high  precision floating
  375.                                  point number.
  376.  
  377.           pointer                A memory  address  in  the
  378.                                  range  0   to   0xFFFFFFFF
  379.                                  (that was a hex number, 32
  380.                                  bits worth).
  381.  
  382.         That was  all  the  possibilities  for  the Lattice
  383.         compiler.  All halfway  decent  C compilers support
  384.         the same set of  simple  types although the precise
  385.         ranges and what  "int"  and  "char"  default to may
  386.         vary.  You can expect  all  compilers for the 68000
  387.         (which the Amiga uses) would  have a similar set of
  388.         ranges and defaults.
  389.  
  390.  
  391.         Well, thats all folks for lesson 1.  In lesson 2 we
  392.         will ponder the mysteries  of  the  full range of C
  393.         operators, complex data types and pointers.  If you
  394.         can stick with it for  that  long you deserve the C
  395.         order of merit.
  396.  
  397. End.
  398.